#include "IO.h"

IO::IO(tvPTR *DATA, tvPTR *DIR)
{
      perror("IO::IO()");
      printf("\t DATA: %X, DIR %X\n", DATA, DIR);
      printf("\t*DATA: %02X, *DIR: %02X\n", *DATA, *DIR);

      PORT = DATA;
      DDR = DIR;
      
      return;
}

IO::~IO()
{
      perror("IO::~IO()");
      return;
}
bool& IO::operator[] (unsigned int i) 
{ 
      //io read
      printf("nonconst %x\n", i); 
      if (i > 7) error(); 
      return data[i]; 
}

const bool& IO::operator[] (unsigned int i) const
{
      printf("const %x\n", i); 
   //   if (i > 7) error(); 
      return data[i]; 
}

tvPTR& IO::operator= (unsigned char  i) 
{
      printf("ass. %x\n", i);
      *PORT = i;
       
       //if (i > 7) error(); 
      return *PORT; 
    // return notpointer;
}   // Assignment operator

tvPTR& IO::read() 
{             
     data[0] = *PORT & 1;
     data[1] = *PORT & 2;
     data[2] = *PORT & 4;
     data[3] = *PORT & 8;
     data[4] = *PORT & 16;
     data[5] = *PORT & 32;
     data[6] = *PORT & 64;
     data[7] = *PORT & 128;     
     return *PORT;        
}

void IO::write() 
{
      *PORT = (data[0] | 
             (data[1] << 1) | 
             (data[2] << 2) | 
             (data[3] << 3) |
             (data[4] << 4) |
             (data[5] << 5) |
             (data[6] << 6) |
             (data[7] << 7));        
             
             return;    
}
tvPTR IO::getbit(unsigned char bit)
{
//both of these are equivalent?
      return ((*PORT >> bit) & 1);
      //return ((*PORT & (1<<bit) >> bit);
}

void IO::set(unsigned char bit)
{
   //  perror("IO::set()");
     
     *PORT = *PORT | (1 << bit);
     return;
}
void IO::clr(unsigned char bit)
{
   //  perror("IO::clr()");
     *PORT = *PORT & ~(1<<bit);
    // printf("\tlike to write\n");
     return;
}
void IO::setoutput(unsigned char bit)
{
     *DDR = *DDR | (1 << bit);
     return;
} 

void IO::setinput(unsigned char bit)
{
    *DDR = *DDR & ~(1<<bit);
    return; 
}
void IO::print()
{
    // printf("print(notpointer) %x  %x\n", notpointer, &notpointer);
     printf("print(PORT) %x  %x\n", *PORT, PORT);
     printf("print(DDR) %x  %x\n", *DDR, DDR);
     for(int i = 0; i < 8; i++) printf("print() data[%d] = %d\n", i, data[i]?1:0);
} 

tvPTR* IO::getPORT()
{
       return PORT;
}
tvPTR* IO::getDDR()
{
       return DDR;
}

/*        
void error()
{
     printf("error\n");
}
*/

/*
class IO
{                
     private:            
     
     public:
     
     bool data[8];
     unsigned char *PORT;
     tvPTR *DDR; //data directions register    
          
     unsigned char notpointer;

     bool& operator[] (unsigned int i) 
     { 
          //io read
          printf("nonconst %x\n", i); 
          if (i > 7) error(); 
          return data[i]; 
     }
    
    const bool &operator[] (unsigned int i) const
    {
          printf("const %x\n", i); 
       //   if (i > 7) error(); 
          return data[i]; 
    }

     unsigned char &operator= (unsigned char  i) 
    {
          printf("ass. %x\n", i);
          *PORT = i;
           
           //if (i > 7) error(); 
          return *PORT; 
        // return notpointer;
    }   // Assignment operator
    
    tvPTR& read() 
    {             
         data[0] = *PORT & 1;
         data[1] = *PORT & 2;
         data[2] = *PORT & 4;
         data[3] = *PORT & 8;
         data[4] = *PORT & 16;
         data[5] = *PORT & 32;
         data[6] = *PORT & 64;
         data[7] = *PORT & 128;     
         return *PORT;        
    }
    
    void write() 
    {
          *PORT = (data[0] | 
                 (data[1] << 1) | 
                 (data[2] << 2) | 
                 (data[3] << 3) |
                 (data[4] << 4) |
                 (data[5] << 5) |
                 (data[6] << 6) |
                 (data[7] << 7));        
                 
                 return;    
    }
    
    void set()
    {
         return;
    }
    void clr()
    {
         return;
    }
    void setouput(unsigned char bit)
    {
         *DDR = *DDR | (1 << bit);
         return;
    } 
    
    void setinput(unsigned char bit)
    {
        *DDR = *DDR & ~(1<<bit);
        return; 
    }
    void print()
    {
         printf("print(notpointer) %x  %x\n", notpointer, &notpointer);
         printf("print(PORT) %x  %x\n", *PORT, PORT);
         for(int i = 0; i < 8; i++) printf("print() data[%d] = %d\n", i, data[i]?1:0);
    }         
    void error(){printf("error\n");}
};
*/
